{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ed48f024",
   "metadata": {},
   "source": [
    "Buisness Problem:\n",
    "\n",
    "Create a product that builds a Competitor Research Report for a company based on its website. The report should summarize:\n",
    "\n",
    "What the company does (Products/Services)\n",
    "\n",
    "Who its customers are\n",
    "\n",
    "Pricing / Plans (if available)\n",
    "\n",
    "Careers / Jobs info (hiring or not)\n",
    "\n",
    "Unique value proposition\n",
    "\n",
    "Any partnerships or notable achievements"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "08a19ee6",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import json\n",
    "from dotenv import load_dotenv\n",
    "from IPython.display import Markdown, display\n",
    "from scraper import fetch_website_contents, fetch_website_links\n",
    "from openai import OpenAI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "fcaa3cb6",
   "metadata": {},
   "outputs": [],
   "source": [
    "load_dotenv(override=True)\n",
    "api_key = os.getenv('OPENAI_API_KEY')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9a1809e4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "API key looks good so far\n"
     ]
    }
   ],
   "source": [
    "if api_key and api_key.startswith('sk-proj-') and len(api_key)>10:\n",
    "    print(\"API key looks good so far\")\n",
    "else:\n",
    "    print(\"There might be a problem with your API key? Please visit the troubleshooting notebook!\")\n",
    "    \n",
    "MODEL = 'gpt-4.1-nano'\n",
    "openai = OpenAI()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "fdea1654",
   "metadata": {},
   "outputs": [],
   "source": [
    "competitor_link_system_prompt = \"\"\"\n",
    "You are provided with a list of links extracted from a company website.\n",
    "Identify links relevent for a Competitor Research Report.\n",
    "\n",
    "Useful links include pages like:\n",
    "-About Page/Company Page\n",
    "-Products/Solutions/Services\n",
    "-Pricing/Plan\n",
    "-Customers or Case Studies\n",
    "-Careers/Jobs\n",
    "-Partners\n",
    "-Press/News\n",
    "\n",
    "Do not include: terms,privacy,email links,login.\n",
    "\n",
    "Respond with a JSON object in format:\n",
    "{\"links\":[{\"type\": \"...\", \"url\": \"https://...\" }]}\n",
    "\"\"\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "b7f16e29",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_links_user_prompt(url):\n",
    "    links = fetch_website_links(url)\n",
    "    prompt = f\"Website: {url}\\nlinks:\\n\" + \"\\n\".join(links)\n",
    "    return prompt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "c287482e",
   "metadata": {},
   "outputs": [],
   "source": [
    "def select_relevent_links(url):\n",
    "    print(f\"Selecting relevent links for (url)\")\n",
    "    response = openai.chat.completions.create(\n",
    "        model= MODEL,\n",
    "        messages=[\n",
    "            {\"role\":\"system\",\"content\":competitor_link_system_prompt},\n",
    "            {\"role\":\"user\",\"content\":get_links_user_prompt(url)}\n",
    "        ],\n",
    "        response_format={\"type\":\"json_object\"}\n",
    "    )\n",
    "    result=json.loads(response.choices[0].message.content)\n",
    "    print(f\"Found {len(result['links'])} relevent links.\")\n",
    "    return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "701da518",
   "metadata": {},
   "outputs": [],
   "source": [
    "def fetch_all_relevant_page_content(url):\n",
    "    landing = fetch_website_contents(url)\n",
    "    relevent = select_relevent_links(url)\n",
    "\n",
    "    result = f\"## Landing Page\\n\\n{landing}\"\n",
    "    for link in relevent['links']:\n",
    "        result += f\"\\n\\n### {link['type'].title()}\\n\"\n",
    "        result += fetch_website_contents(link['url'])\n",
    "    return result[:5000] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "f0285148",
   "metadata": {},
   "outputs": [],
   "source": [
    "competitor_report_system_prompt = \"\"\"\n",
    "You are an AI that creates a easily understandable Competitor Analysis Report for a company.\n",
    "Use headings and bullet points in clean Markdown.\n",
    "\n",
    "Include these sections when possible:\n",
    "\n",
    "- Overview of the Company\n",
    "- Products / Services\n",
    "- Customers & Use Cases\n",
    "- Pricing / Plans\n",
    "- Careers / Hiring\n",
    "- Unique Strengths / Value Proposition\n",
    "- Risks or Weaknesses (if visible)\n",
    "\n",
    "Keep it professional and factual.\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "64e9cd23",
   "metadata": {},
   "outputs": [],
   "source": [
    "def generate_competitor_report(company_name, url):\n",
    "    content = fetch_all_relevant_page_content(url)\n",
    "\n",
    "    response = openai.chat.completions.create(\n",
    "        model=\"gpt-4.1-mini\",\n",
    "        messages=[\n",
    "            {\"role\": \"system\", \"content\": competitor_report_system_prompt},\n",
    "            {\"role\": \"user\", \"content\": f\"Company: {company_name}\\n\\nWebsite Content:\\n{content}\"}\n",
    "        ],\n",
    "    )\n",
    "    report = response.choices[0].message.content\n",
    "    display(Markdown(report))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "ee38f130",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Selecting relevent links for (url)\n",
      "Found 6 relevent links.\n"
     ]
    },
    {
     "data": {
      "text/markdown": [
       "# Competitor Analysis Report: Notion\n",
       "\n",
       "---\n",
       "\n",
       "## Overview of the Company\n",
       "- **Name:** Notion  \n",
       "- **Website:** https://www.notion.so  \n",
       "- **Industry:** Productivity software / Workspace collaboration / AI-powered tools  \n",
       "- **Mission:** To streamline work by combining teams and AI agents in a single workspace, reducing busywork and enhancing productivity.  \n",
       "- **User Base:** Over 100 million users worldwide  \n",
       "- **Recognition:**  \n",
       "  - #1 knowledge base (G2, 3 years running)  \n",
       "  - #1 AI enterprise search (G2)  \n",
       "  - #1 rated AI writing (G2)  \n",
       "\n",
       "---\n",
       "\n",
       "## Products / Services\n",
       "- **Notion 3.0:** Latest version emphasizing AI-enhanced productivity and collaboration.\n",
       "- **Notion Agent:**  \n",
       "  - AI assistant that performs tasks on behalf of users.  \n",
       "  - Automates busywork tasks — what previously took days can be done in minutes.  \n",
       "  - Collaborates with team members like a built-in power user.  \n",
       "  - Personalizes behavior based on individual user preferences.  \n",
       "  - Searches across all data sources: pages, messages, files, and the web.  \n",
       "  - Custom Agents feature coming soon to automate repetitive tasks.\n",
       "\n",
       "- **Enterprise Search:** Unified search tool covering all company data and files — one search for everything.\n",
       "- **AI Meeting Notes:** Automatically creates perfect meeting notes.\n",
       "- **Flexible Workflows:** Tools to manage projects of any size, emphasizing flexibility and scalability.\n",
       "- **Unified Workspace:** Combines teams and tools under one platform to increase efficiency and reduce the number of apps needed.\n",
       "\n",
       "---\n",
       "\n",
       "## Customers & Use Cases\n",
       "- **Target Users:** Teams and enterprises looking to centralize knowledge, automate workflows, and leverage AI to increase productivity.\n",
       "- **Use Cases:**  \n",
       "  - Knowledge management platform  \n",
       "  - AI-powered project management and task automation  \n",
       "  - Enterprise-wide search and data retrieval  \n",
       "  - Collaborative document editing and note-taking  \n",
       "  - Meeting transcription and note automation  \n",
       "- **Customer Testimonials:**  \n",
       "  - A 7-person team can function like 70 with Notion Agent.  \n",
       "  - Clients report 3x faster workflows and improved team collaboration.  \n",
       "  - Notion cited as a competitive AI-native tool giving advantages in productivity.\n",
       "\n",
       "---\n",
       "\n",
       "## Pricing / Plans\n",
       "- The website prompts visitors to \"See pricing plans,\" indicating tiered offerings but specific prices are not visible in the content.\n",
       "- Emphasis on a free access tier (\"Get Notion free\") and the option to request a demo suggests freemium and enterprise pricing models.\n",
       "- Value calculator tools are available to demonstrate savings from consolidating tools into Notion.\n",
       "\n",
       "---\n",
       "\n",
       "## Careers / Hiring\n",
       "- No direct content about careers or hiring was included in the provided text.\n",
       "- Given company scale (100M+ users) and continuous product development (Notion 3.0, AI Agents), likely ongoing recruitment for product, AI, engineering, and support roles.\n",
       "\n",
       "---\n",
       "\n",
       "## Unique Strengths / Value Proposition\n",
       "- **All-in-One Workspace:** Combines knowledge management, AI automation, project management, and enterprise search in one platform.  \n",
       "- **AI Integration:** Advanced AI agents that understand user behavior and automate tasks effectively.  \n",
       "- **Personalization:** Customizable AI assistants that adapt to individual workflows.  \n",
       "- **Trusted by Millions:** Large user base and high ratings on influential platforms (G2).\n",
       "- **Enterprise Focus:** Features like enterprise search and automation appeal to large organizations seeking productivity gains.  \n",
       "- **Proven Efficiency Gains:** Case studies and testimonials report significant productivity improvements and streamlined workflows.\n",
       "\n",
       "---\n",
       "\n",
       "## Risks or Weaknesses\n",
       "- **Pricing Transparency:** Pricing models and plans are not immediately clear from available content, which may pose a barrier for some prospects.  \n",
       "- **Heavy Dependence on AI:** While AI agents offer unique advantages, they may also create reliance risks if functionality is imperfect or training data is biased.  \n",
       "- **Competition:** The productivity space is crowded with well-established tools (e.g., Microsoft Teams, Asana, Slack, Google Workspace) that also aggressively add AI capabilities.  \n",
       "- **Adoption Curve:** Transitioning teams from their current toolchains can require significant change management.\n",
       "\n",
       "---\n",
       "\n",
       "# Summary\n",
       "Notion positions itself as a transformative AI-first workspace solution designed to reduce busywork through intelligent automation and unified knowledge management. Its strengths lie in deep AI integration, a large user base, and an all-in-one platform approach targeting both small teams and enterprises. However, the company operates in a highly competitive sector where clear pricing transparency and continued innovation are vital for sustained growth.\n",
       "\n",
       "---\n",
       "\n",
       "*Report compiled based on publicly available website content as of June 2024.*"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "generate_competitor_report(\"Notion\", \"https://www.notion.com/\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9a05bcc0",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
